<template>
  <div class="app-container">
    <div class="custom_card_0">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" size="small">
        <el-form-item label="出租方" prop="enterpriseCertificationName">
          <el-input v-model="queryParams.enterpriseCertificationName" placeholder="请输入出租方名称" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="承租方" prop="memberName">
          <el-input v-model="queryParams.memberName" placeholder="请输入承租方" clearable
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="合同分类" prop="businessType">
          <el-select v-model="queryParams.businessType" placeholder="请选择合同类型" clearable>
            <el-option label="租车合同" :value="1"/>
            <el-option label="续租合同" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="合同状态">
          <el-select v-model="queryParams.contractStatusList" placeholder="请选择合同状态" clearable multiple style=" width: 200px">
            <el-option v-for="dict in dict.type.contract_status" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>


    <div class="custom_card">
      <el-row class="mb8" :gutter="10">

        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="handleQuery"
        ></right-toolbar>
      </el-row>
      <el-table
        v-loading="loading"
        :data="basicList"
        stripe
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }" border>
        <template slot="empty">
          <el-empty :image-size="50" description="暂无合同信息"></el-empty>
        </template>
        <el-table-column
          align="center"
          prop="contractNo"
          label="合同编号"
          min-width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="businessType"
          label="合同类型"
          min-width="180">
          <template v-slot="{row}">
            {{row.businessType|contractType}} - {{row.contractSource==1?'线下合同':'线上合同'}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="orderNo"
          label="关联订单"
          min-width="180">
          <template v-slot="{row}">
            <el-button type="text" @click="goOrderDetail(row)">{{row.orderNo}}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="businessType"
          label="合同状态"
          min-width="180">
          <template v-slot="{row}">
            <dict-tag :options="dict.type.contract_status" :value="row.contractStatus"/>
          </template>

        </el-table-column>
        <el-table-column
          align="center"
          prop="enterpriseCertificationName"
          label="出租方"
          min-width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="memberName"
          label="承租方"
          min-width="180">
        </el-table-column>
        <el-table-column
          align="center"
          prop="takeStoreName"
          label="门店"
          min-width="180">
        </el-table-column>
        <el-table-column
          prop="rent"
          align="center"
          label="租金(元)"
          min-width="180">
        </el-table-column>
        <el-table-column
          prop="deposit"
          align="center"
          label="车辆押金(元)"
          min-width="180">
        </el-table-column>
        <el-table-column
          prop="illegalDeposit"
          align="center"
          label="违章押金(元)"
          min-width="180">
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="left">
          <template v-slot="{ row }">
            <el-button size="small" type="text"
                       @click="handleOperate(1,row)">查看
            </el-button>
            <el-button v-if="row.contractStatus==1" size="small" type="text"
                       @click="handleOperate(2,row)">出租方签署
            </el-button>
            <el-button v-if="row.contractStatus==2" size="small" type="text"
                       @click="handleOperate(3,row)">承租方签署
            </el-button>


          </template>
        </el-table-column>
      </el-table>

      <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                  @pagination="getList"/>
    </div>

  </div>
</template>

<script>

import {contractList, updateContractStatus} from "@/api/contract/contract";
import {contractType} from "@/utils/filters";

export default {
  name:'ContractList',
  components: {},
  props: {},
  dicts: ['contract_status'],
  data() {
    // 这里存放数据
    return { // 加载标识
      loading: false,
      showSearch:true,
      // 总条数
      total: 0,
      // 工单数据
      basicList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseCertificationName:'',
        memberName:'',
        businessType:'',
        contractStatusList:[]
      },
    }
  },
  // 计算属性 类似于 data 概念
  computed: {
  },
  // 方法集合
  methods: {
    resetQuery(){
      this.$refs.queryForm.resetFields();
      this.queryParams.contractStatusList=[];
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      contractList(this.queryParams).then(res=>{
        this.loading =false;
        this.total = res.total;
        this.basicList = res.rows;
      }).catch(()=>{
        this.loading = false;
      })
    },
    /*操作按钮点击事件*/
    handleOperate(type, row) {
      const currentId = row.id;
      switch (type) {
        case 1://查看
          this.$router.push({name:'contractDetail', query:{contractId:currentId}});
          break
        case 2://出租方签署
          this.updateContractStatus(currentId, 1, '确定发起合同签署吗?');
          break
        case 3://承租方签署
          this.updateContractStatus(currentId, 2, '确定发起合同签署吗?');
          break

        default:
          console.log('未知')
          break
      }
    },

    /*修改状态*/
    updateContractStatus(contractId,status,msg) {
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(() => {
        this.loading = true;
        updateContractStatus({
          id : contractId,
          contractStatus : status
        }).then(res=>{
          this.loading = false;
          this.handleQuery();
        })
      })

    },

    /*查看订单*/
    goOrderDetail(row){
      this.$router.push({
        path: '/orderBasic/detail',
        query: { orderNo: row.orderNo }
      })
    }
  },
  // 生命周期 - 创建完成（可以访问当前this 实例）
  created() {
    this.getList();
  },
  // 生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {

  },
  activated() {
    this.getList();
  },

}
</script>

<style lang="scss" scoped>
</style>
